package net.begincode.searchCommon.util;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @author hlj~宇晗
 * @create 2017-03-24 21:17
 **/
//封装了连接池，url，get和post
public class HttpUtil {
	// 将最大连接数增加到
	public static final int MAX_TOTAL = 700;
	// 将每个路由基础的连接增加到
	public static final int MAX_ROUTE_TOTAL = 500;
	//设置超时时间
	public static final int REQUEST_TIMEOUT = 5 * 1000;
	public static final int REQUEST_SOCKET_TIME = 5 * 1000;
	//public static  String[] cx;
	public static CloseableHttpClient getHttpClient(){
		//定义连接池
		PoolingHttpClientConnectionManager pm
				=new PoolingHttpClientConnectionManager();
		//定义连接池参数
		pm.setMaxTotal(MAX_TOTAL);
		pm.setDefaultMaxPerRoute(MAX_ROUTE_TOTAL);
		RequestConfig requestConfig=RequestConfig
				.custom().setConnectTimeout(REQUEST_TIMEOUT)
				.setSocketTimeout(REQUEST_SOCKET_TIME).build();
		//设置重定向策略
		LaxRedirectStrategy redirectStrategy=
				new LaxRedirectStrategy();

		return HttpClients.custom().setConnectionManager(pm)
				.setDefaultRequestConfig(requestConfig)
				.setRedirectStrategy(redirectStrategy).build();
	}

	//得到URI
	public static String getURI(String status,String keyword,int count,int page) throws URISyntaxException {
		//cx列表
		String[] cx_begincode = {
				"014593608606439902528:szoyjnrqayq",
				"014593608606439902528:lnmixnyolra",
				"014593608606439902528:gfd1iffizmk"};
		String[] cx_baiduyun = {
				"014593608606439902528:oxxrexthozw",
				"014593608606439902528:mvojwt-xxa0",
				"014593608606439902528:s7lci0ae1ew"
				};
		String[] cx =null;
		if ("begincode_search".equals(status)) {//设定0为站内搜索
			cx = cx_begincode;
		} else if("baiduyun_search".equals(status)){//设定1为百度云搜索
			cx = cx_baiduyun;
			System.out.println("cx="+cx);
		}
		//System.out.println("cx= "+cx);
		//定义传递参数
		//URI uri=new URIBuilder().setScheme("https")
		//		.setHost("www.googleapis.com")
		//		.setPath("/customsearch/v1element")
		//		.setParameter("key","AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY")
		//		.setParameter("rsz","filtered_cse")
		//		.setParameter("num","10")
		//		.setParameter("hl","zh_CN")
		//		.setParameter("prettyPrint","false")
		//		.setParameter("source","gcsc")
		//		.setParameter("gss",".com")
		//		.setParameter("sig","0c3990ce7a056ed50667fe0c3873c9b6")
		//		.setParameter("cx",cx[count-1])
		//		.setParameter("q",keyword)
		//		.setParameter("start", Integer.toString(page)).build();
		String uri = "https://www.googleapis.com/customsearch/v1element?" +
				"key=AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY&rsz=filtered_cse&" +
				"num=10&hl=zh_CN&prettyPrint=false&source=gcsc&gss=.com&" +
				"sig=0c3990ce7a056ed50667fe0c3873c9b6&" +
				"cx=" + cx[count - 1] +
				"&q=" + keyword +"&start="+page;

		return uri;
	}
	//封装了get方式，无须创建httpclient
	public static  CloseableHttpResponse doGet(String uri) throws IOException {
		HttpGet httpGet=new HttpGet(uri);
		return getHttpClient().execute(httpGet);
	}
	//封装了post方式
	public static CloseableHttpResponse doPost(String uri) throws IOException {
		HttpPost httpPost=new HttpPost(uri);
		return getHttpClient().execute(httpPost);
	}
}
